import { getToken } from "./util/auth"
import store from './store/index'
import router from './router/index'
window.$axios = require('axios');
window.$axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
console.log("process.env.VUE_APP_BASE_URL=========", process.env.VUE_APP_BASE_URL)
window.$axios.defaults.baseURL = process.env.VUE_APP_BASE_URL
window.$axios.defaults.timeout = 5000   // 请求超时时间
window.$moment = require('moment');
window._ = require('lodash');


// 添加请求拦截器
window.$axios.interceptors.request.use(
    config => {
      if (store.getters.token) {
        config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
        // config.headers['ua'] = 'qianheyi/1.0.1';
      }
      return config;
    },
    err => {
      return Promise.reject(err);
    });

// 添加响应拦截器
window.$axios.interceptors.response.use(
    response => {
      // 对响应数据做点什么
      let res = response.data
      if (res.status && res.status == '10001') {
        store.dispatch('FedLogOut').then(() => {
          router.push("/login") // 登录页
        }).catch(error => {
            console.log("axios response error ===", error)
        })
        
        return Promise.reject(res)
      } else {
        return response.data
      }
    },
    error => {
      // 对响应错误做点什么
      return Promise.reject(error);
    });